<template>
  <div class="login-container">
    <!-- 左侧品牌展示区 -->
    <div class="brand-side">
      <div class="brand-content">
        <img 
          src="../../picture/生成校园小程序 logo.png"
          class="brand-logo"
          alt="校园帮logo"
        />
        <h1 class="brand-title">校园帮</h1>
        <p class="brand-desc">智慧校园生活平台</p>
      </div>
    </div>

    <!-- 右侧登录区域 -->
    <div class="login-form-container">
      <div class="login-card">
        <!-- 登录表单 -->
        <div class="login-form">
          <div class="form-header">
            <h2 class="login-title">账号登录</h2>
            <div class="title-divider"></div>
          </div>

          <!-- 简化版表单 -->
          <el-form ref="formRef" :model="loginForm" :rules="rules" class="form-content">
            <!-- 账号输入框 -->
            <el-form-item prop="account" class="input-item">
              <el-input 
                v-model="loginForm.account"
                placeholder="请输入账号"
                class="custom-input"
                prefix-icon=""
              >
              </el-input>
            </el-form-item>

            <!-- 密码输入框 -->
            <el-form-item prop="password" class="input-item">
              <el-input 
                v-model="loginForm.password"
                placeholder="请输入密码"
                type="password"
                class="custom-input"
                prefix-icon=""
              >
              </el-input>
            </el-form-item>

            <el-button type="primary" class="login-btn" @click="handleLogin">
              立即登录
            </el-button>

            <div class="register-link">
              <span>还没有账号？</span>
              <!-- 将a标签替换为router-link -->
              <router-link to="/register" class="register-text">立即注册</router-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router' // 导入路由
import { errorAlert } from '@/utils/alert' // 导入自定义MessageBox

// 初始化路由实例
const router = useRouter()

// 表单数据模型
const loginForm = ref({
  account: '',
  password: ''
})

// 表单验证规则
const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ]
}

// 表单引用
const formRef = ref(null)

// 登录处理函数
const handleLogin = async () => {
  try {
    // 表单验证
    await formRef.value.validate()
    const baseUrl = process.env.VUE_APP_API_BASE.trim();
    const loginUrl = `${baseUrl}/api/auth/login`;

    // 发送登录请求
    const response = await axios.post(loginUrl, {
      account: loginForm.value.account,
      password: loginForm.value.password
    })



    if (response.data.success) {
      // 登录成功，存储token（如果有）
      if (response.data.token) {
        localStorage.setItem('token', response.data.token)
      }
      // 检查用户信息是否完整
      if (response.data.infoComplete === false) {
        // 信息不完整，跳转到信息完善页面
        router.push('/profile-complete');
      } else {
        // 信息完整，跳转到首页
        router.push('/home');
      }
    } else {
      errorAlert(response.data.message, '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    if (error.name === 'ValidationError') {
      // 表单验证失败，Element Plus会自动提示
    } else if (error.response) {
      errorAlert(`服务器错误：${error.response.status}`, '错误')
    } else {
      errorAlert('网络连接异常，请稍后重试', '错误')
    }
  }
}
</script>

<style scoped>
/* 基础布局样式 */
.login-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="%23ffffff" opacity="0.05"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  pointer-events: none;
}

/* 左侧品牌区样式 */
.brand-side {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  position: relative;
  z-index: 1;
}

.brand-content {
  text-align: center;
  transform: translateY(-20px); /* 与注册页保持相同的垂直位置 */
}

/* 品牌logo样式 */
.brand-logo {
  width: 120px;
  height: 120px;
  margin-bottom: 30px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  padding: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.brand-logo:hover {
  transform: scale(1.05);
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.15);
}

.brand-title {
  font-size: 48px;
  margin-bottom: 20px;
  font-weight: 800;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  background: linear-gradient(45deg, #ffffff, rgba(255, 255, 255, 0.8));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.brand-desc {
  font-size: 18px;
  opacity: 0.9;
  font-weight: 400;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  max-width: 300px;
  line-height: 1.6;
}

/* 右侧表单区域 */
.login-form-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  position: relative;
  z-index: 1;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 50px 40px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  border-radius: 24px;
  pointer-events: none;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 35px 70px rgba(0, 0, 0, 0.2);
}

/* 表单标题样式 */
.form-header {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  z-index: 2;
}

.login-title {
  font-size: 28px;
  color: #1f2937;
  font-weight: 700;
  margin: 0 0 16px 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.title-divider {
  width: 60px;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  margin: 0 auto;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

/* 现代化输入框样式 */
.input-item {
  margin-bottom: 24px;
  position: relative;
  z-index: 2;
}

:deep(.custom-input .el-input__wrapper) {
  background: rgba(255, 255, 255, 0.8) !important;
  backdrop-filter: blur(10px) !important;
  border: 1px solid rgba(102, 126, 234, 0.2) !important;
  border-radius: 16px !important;
  padding: 0 20px !important;
  height: 56px !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05) !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
}

:deep(.custom-input .el-input__inner) {
  font-size: 16px !important;
  color: #1f2937 !important;
  font-weight: 500 !important;
  background: transparent !important;
  border: none !important;
  height: 56px !important;
  line-height: 56px !important;
  outline: none !important;
}

:deep(.custom-input .el-input__wrapper),
:deep(.custom-input .el-input__inner),
:deep(.custom-input .el-input__wrapper *) {
  outline: none !important;
}

:deep(.custom-input .el-input__inner::placeholder) {
  color: #9ca3af !important;
  font-weight: 400 !important;
}

/* 聚焦状态 */
:deep(.custom-input .el-input__wrapper:focus-within) {
  border-color: #667eea !important;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1), 0 8px 20px rgba(102, 126, 234, 0.15) !important;
  background: rgba(255, 255, 255, 0.95) !important;
}

/* 悬停状态 */
:deep(.custom-input .el-input__wrapper:hover:not(.is-focus)) {
  border-color: rgba(102, 126, 234, 0.4) !important;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08) !important;
  background: rgba(255, 255, 255, 0.9) !important;
}

/* 验证错误状态 */
:deep(.el-form-item.is-error .custom-input .el-input__wrapper) {
  border-color: rgba(239, 68, 68, 0.5) !important;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1) !important;
}

/* 移除重复定义的样式 */
/* 请删除以下代码块 */
/*
.custom-input .el-input__inner {
  height: 46px;
  border-radius: 8px;
  padding-left: 40px;
}
*/

/* 登录按钮样式 */
:deep(.login-btn) {
  width: 100% !important;
  height: 56px !important;
  border-radius: 16px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  transition: all 0.3s ease !important;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;
  position: relative !important;
  z-index: 2 !important;
  margin-top: 8px !important;
}

:deep(.login-btn:hover) {
  transform: translateY(-3px) !important;
  box-shadow: 0 15px 35px rgba(102, 126, 234, 0.6) !important;
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%) !important;
}

:deep(.login-btn:active) {
  transform: translateY(-1px) !important;
}

/* 注册链接样式 */
.register-link {
  text-align: center;
  margin-top: 24px;
  font-size: 15px;
  color: #6b7280;
  position: relative;
  z-index: 2;
}

.register-text {
  color: #667eea;
  margin-left: 8px;
  text-decoration: none;
  font-weight: 600;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.register-text:hover {
  color: #5a6fd8;
  text-decoration: underline;
}
/* 响应式调整 */
@media (max-width: 992px) {
  .brand-side {
    display: none;
  }
  .login-form-container {
    flex: 1;
  }
}

/* 密码框图标位置调整 */
:deep(.el-form-item .custom-input .el-input__suffix) {
  position: absolute !important;
  right: 20px !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
  z-index: 10 !important;
}

:deep(.custom-input.has-suffix .el-input__wrapper) {
  padding-right: 50px !important;
}
</style>

methods: {
  async handleLogin() {
    try {
      const response = await this.$api.login(this.loginForm);
      if (response.success) {
        // 存储token到localStorage
        localStorage.setItem('token', response.token);
        this.$router.push('/square'); // 跳转到交流广场
      } else {
        errorAlert(response.message, '登录失败');
      }
    } catch (error) {
      errorAlert('网络错误，请稍后重试', '错误');
    }
  }
}
